/*
 * Filename : RespawnTimer.java
 * Description : **COMPLETE**
 * History  : Date               Engineer       Description
 *            ---------          ------------   --------------------------------
 *            September 10, 2007      waitek         Initial creation.
 */

package com.lct.eq2.data;

import java.awt.Toolkit;
import java.io.Serializable;
import java.util.Comparator;
import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;
import com.lct.eq2.gui.MessageBox;

public class RespawnTimer extends Thread implements Runnable, Serializable, Comparator {
    static final long serialVersionUID = 7442876267328467882L;
    
    private String combatantName;
    private String doFileCommandsPath;
    private Duration respawnTimer;
    private Duration totalDuration;
    private boolean running = true;
    private boolean respawnPopup = false;
    private boolean respawnBeep = false;
    private boolean isThrowaway = false;
    
    /**
     * Creates a new instance of RespawnTimer
     */
    public RespawnTimer(String name, Duration duration, boolean popup, boolean respawnBeep, String path) {
        this.respawnPopup = respawnPopup;
        this.respawnBeep = respawnBeep;
        this.combatantName = name;
        this.respawnTimer = duration;
        Duration instance2 = new Duration();
        instance2.setTotalSeconds(duration.getDurationInSeconds());
        this.totalDuration = instance2;
    }
    
    public RespawnTimer() {
    }
    
    public void setThrowaway(boolean throwaway) {
        this.isThrowaway = throwaway;
    }
    
    public boolean isThrowaway() {
        return this.isThrowaway;
    }
    
    public void reset() {
        this.stopRunning();
        this.respawnTimer.setTotalSeconds(0);
    }
    
    public Duration getTotalDuration() {
        return this.totalDuration;
    }
    
    public void stopRunning() {
        this.running = false;
    }
    
    public long getRemainingTime() {
        return this.respawnTimer.getDurationInSeconds();
    }
    
    public void run() {
        try {
            long seconds = this.respawnTimer.getDurationInSeconds();
            while ((running) && (this.respawnTimer.getDurationInSeconds() > 0)) {
                seconds--;
                this.respawnTimer.setTotalSeconds(seconds);
                this.currentThread().sleep(1000);
            }
            
            if (running) {
                if (respawnBeep) {
                    Toolkit.getDefaultToolkit().beep();
                }
                if (respawnPopup) {
                    //MessageBox mb = new MessageBox();
                    //JOptionPane.showMessageDialog(this, "Testing", "Title", JOptionPane.INFORMATION_MESSAGE);
                    //mb.doOkCancelDialog("Respawn Alert!", this.combatantName + "'s respawn timer is up!");
                }
            }
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
    
    public String getCombatantName() {
        return this.combatantName;
    }
    
    public String toString() {
        String rval = null;
        if (this.respawnTimer.getDurationInSeconds() > 0) {
            rval = this.combatantName + " - [" + this.respawnTimer.getDuration() + "] until respawn.";
        } else {
            rval = this.combatantName + " should be up.";
        }
        return rval;
    }

    public int compare(Object o1, Object o2) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)o1;
        RespawnTimer r1 = (RespawnTimer)node.getUserObject();
        node = (DefaultMutableTreeNode)o2;
        RespawnTimer r2 = (RespawnTimer)node.getUserObject();
        int rval = 0;
        if (r1.getCombatantName().compareToIgnoreCase(r2.getCombatantName()) > 0) {
            rval = 1;
        } else if (r1.getCombatantName().compareToIgnoreCase(r2.getCombatantName()) < 0) {
            rval = -1;
        } else {
            rval = 0;
        }
        return rval;
    }
}
